package com.orange.core.pojo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.util.Constants;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class PageReqDto<C> implements Serializable {

    private long page = 1;
    private long size = Constants.DEFAULT_PAGE_SIZE;
    private long offset = 0;
    private C condition;

    public PageReqDto<C> setPage(long page) {
        this.page = page;
        if (this.page < 1) {
            this.page = 1;
        }
        return this;
    }

    public PageReqDto<C> setSize(long size) {
        this.size = size;
        if (this.size < 1 || this.size > Constants.MAX_PAGE_SIZE) {
            this.size = Constants.DEFAULT_PAGE_SIZE;
        }
        return this;
    }

    public long getOffset() {
        return (getPage() - 1) * getSize();
    }

    public <T> Page<T> toPage() {
        return new Page<>(page, size, 0);
    }
}
